(function() {
    angular.module('app').component('darenTieComponent', {
        templateUrl: 'app/controllers/redNet.controller/daren.tie.component/daren.tie.component.html',
        controller: darenTieComponent,
        controllerAs: 'vm'
    });


    darenTieComponent.$inject = ['darenTieComponentService', '$rootScope', '$scope', '$loading'];

    function darenTieComponent(darenTieComponentService, $rootScope, $scope, $loading) {

        var vm = this;

        vm.filter = {
            sortColumn: '',

            darenUid: '',

            sort: 'down',

            newPageNumber: 1, //page
            count: 10, //条数
            begin: 1
        }

        vm.daren = {};

        vm.table = {
            count: 0,
            ties: []
        }

        vm.pageChanged = _pageChanged; //function

        vm.getDarenTieProduct = _getDarenTieProduct; //function

        var clearFun = $rootScope.$on('redNet_daren_tie_EV', function(e, daren) {

            vm.daren = daren;
            
            vm.filter = {
                sortColumn: '',

                darenUid: '',

                sort: 'down',

                newPageNumber: 1, //page
                count: 10, //条数
                begin: 1
            }


            vm.filter.darenUid = daren.darenUid;



            _init();
        });


        function _init() {

            $loading.start('darenTie');

            var json = angular.copy(vm.filter);

            darenTieComponentService.getDarenTie(json).then(function(data) {

                vm.table.count = data.count;

                vm.table.ties = data.darenFeeds;

                $loading.finish('darenTie');

            }, function(err) {});


        }

        function _pageChanged(newPageNumber) {
            vm.filter.newPageNumber = newPageNumber;
            vm.filter.begin = (vm.filter.newPageNumber - 1) * vm.filter.count;
            _init();
        }

        function _getDarenTieProduct(tie) {

            $rootScope.$emit('redNet_daren_tie_product_EV', tie, angular.copy(vm.daren));
        }

    }

})();
